angular.module('mall-logo', [])

.controller 'MallLogoCtrl', ($ionicLoading,$scope, $state, $http,showToast, $ionicHistory, $stateParams, newAddress, wfCache, $ionicViewSwitcher, $ionicPopup, userData, $log,$cordovaCamera,mallInfo) ->
    logger = $log.getInstance 'MallLogoCtrl'
    _.extend $scope,
        logo:
            img:mallInfo.mall.logo
        goBack: ->
            $ionicHistory.goBack()
        goSave: ->
            if $scope.logo.img.indexOf('http')==0
                showToast '请选择logo!'
                return
            $ionicLoading.show()
            $http.post('/seller/', {action: 'submitlogo', userid: userData.getToken(), logo: $scope.logo.img})
            .success (resp) ->
                if resp?.status == '0'
                    showToast 'logo上传成功!'
                else
                    showToast 'logo上传失败!'
            .error ->
                showToast '您的网络又调皮了!'
            .finally ->
                $ionicLoading.hide()

        imgAdd: ->
            options =
                quality: 50
                destinationType: Camera.DestinationType.DATA_URL
                sourceType: Camera.PictureSourceType.PHOTOLIBRARY
                allowEdit: true
                encodingType: Camera.EncodingType.JPEG
                popoverOptions: CameraPopoverOptions
                saveToPhotoAlbum: false
            $cordovaCamera.getPicture(options).then ((imageData) ->
                $scope.logo = {img:"data:image/jpeg;base64," + imageData}
            ), (err) -> null
